// pages/product/detail/detail-unit/detail-unit.js
import ProductService from "../../../../class/service/ProductService";
import SmsService from "../../../../class/service/SmsService";
import MallService from "../../../../class/service/MallService";
import Router from "../../../../class/Router";
import Tips from "../../../../utils/Tips";

const util = require("../../../../utils/util.js")
const storage = require("../../../../utils/storage.js")
const constant = require("../../../../constant.js")
const notification = require("../../../../utils/WxNotificationCenter.js");

const productService = new ProductService();
const smsService = new SmsService();
const mallService = new MallService();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    init: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    that.setData({
      skuId: options.id
    })

    var unionid = wx.getStorageSync(storage.keys.unionid);
    that.setData({
      unionid: unionid
    })

    var openid = wx.getStorageSync(storage.keys.openid);
    that.setData({
      openid: openid
    })

    var mobile = wx.getStorageSync(storage.keys.mobile);
    that.setData({
      mobile: mobile
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    var imgsrc = 'http://aipunet.qiniu.edooschool.com/downpour-wechat.png?' + new Date().getTime();
    this.setData({
      img: imgsrc
    })

    if (that.data.mobile) {
      that.initData()
    } else {
      that.setData({
        init: false,
        needMobile: true
      })
    }
  },

  initData: function () {
    var that = this;

    Tips.loading();

    mallService.detail(that.data.skuId).then(data => {
      //Tips.loaded();
      
      console.log(data)
      
      that.setData({
        "init": true,
        "sku": data.sku,
        "courses": data.courses
      })

      productService.listByPersion(that.data.unionid).then(data => {
        Tips.loaded();
        that.setData({
          "init": true,
          "userCourses": data.userCourses,
          "selfCourse": data.userCoursesSize
        })

        that.mergeCourse();
      }, res => {
        Tips.error("获取练习失败");
        that.setData({
          "init": true,
          "selfCourse": 0
        })
      })

    }, res => {
      Tips.error("获取商品详情失败");
      that.setData({
        "init": true
      })
    })

  },

  mergeCourse: function () {
    var that = this;
    var userCourses = that.data.userCourses;
    var courses = that.data.courses;
    var result = [];
    for (var i in courses) {
      var course = courses[i];
      if (userCourses && userCourses[course.id] != null) {
        course['paid'] = true
      }
      result.push(course)
    }

    that.setData({
      courses: result
    })

  },

  /**
   * 跳转到详情页面
   */
  goToDetials: function (e) {
    var id = e.currentTarget.dataset.id;
    var paid = e.currentTarget.dataset.paid;
    if (paid) {
      wx.navigateTo({
        url: '../../../../pages/product/unit-list/unit-list?id=' + id
      });
    } else {
      Tips.confirm("您还未购买该课程,点击确定去购买~", { skuId: id }, "温馨提示").then(data => {
        console.log(data);
        wx.navigateTo({
          url: '../../../../pages/mall/mall',
        })
      }, res => {
        console.log(res);
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})